package com.atguitu.campus.renwendemo.Controller;

import com.atguitu.campus.renwendemo.dto.PageQueryBaseDto;
import com.atguitu.campus.renwendemo.entity.Role;
import com.atguitu.campus.renwendemo.service.IRoleService;
import com.atguitu.campus.renwendemo.utils.AjaxResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/role")
@Tag(name = "角色管理", description = "角色相关的API接口")
public class RoleController {

    @Autowired
    private IRoleService iRoleService;

    // 查询所有
    @Operation(summary = "获取所有角色", description = "返回所有角色的列表")
    @PostMapping("/list")
    public AjaxResult list() {

        return AjaxResult.success(iRoleService.list());
    }

    // 分页查询用户
    @PostMapping("/page")
    public AjaxResult page(@RequestBody PageQueryBaseDto dto) {
        Page<Role> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getKeyword())){
            queryWrapper.likeRight("name",dto.getKeyword());
        }
        return AjaxResult.success(iRoleService.page(page,queryWrapper));
    }



    // 添加用户
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Role role) {

        return AjaxResult.success(iRoleService.save(role)) ;
    }

    // 更新用户
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Role role) {
        return AjaxResult.success(iRoleService.updateById(role));
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return AjaxResult.success(iRoleService.removeById(id));
    }
}
